/**
 *
 *
 */
#include <iostream>
#include <vector>

using namespace std;

class Student
{
public:
    Student() = default;
    Student(std::string n) : name{std::move(n)}
    {
        // printf("name=%s\n", name.c_str());
    }
    Student(std::string n, int a) : name{n}, age{a} {}

    // // 拷贝构造函数，深拷贝
    Student(const Student &stu)
    {
        printf("拷贝构造函数，深拷贝\n");
        name = stu.name;
        age = stu.age;
    }
    // 移动构造函数，浅拷贝
    Student(Student &&stu)
    {
        printf("移动构造函数，浅拷贝\n");
        name = std::move(stu.name);
        age = std::move(stu.age);
    }

    std::string name;
    int age;
};

void test01()
{
    Student stu1{"alex"};
    printf("stu1.name=%s\n", stu1.name.c_str());

    // printf("1----------\n");
    // Student stu2(stu1); // 调用拷贝构造

    printf("2----------\n");
    Student stu3(Student{"bill"}); // 优先调用移动构造函数，若没有定义移动构造，则调用拷贝构造（参数必须是const &）
    printf("stu3.name=%s\n", stu3.name.c_str());

    printf("3----------\n");
    Student stu4(std::move(stu1)); // 同上
    printf("stu1 addr=%x\n", (int *)&stu1);
    printf("stu4 addr=%x\n", (int *)&stu4);

    printf("stu1.name=%s\n", stu1.name.c_str());
    printf("stu4.name=%s\n", stu4.name.c_str());
}

/**
 *
 *
 */
vector<int> create_list()
{
    vector<int> list;
    printf("%x \n", (int *)&list);
    list.push_back(1);
    list.push_back(2);

    return list;
}
void test02()
{
    vector<int> list = create_list();
    printf("%x \n", (int *)&list);
}

/**
 *
 *
 */
vector<Student> create_list_student()
{
    vector<Student> list;
    list.reserve(10);
    printf("%x \n", (int *)&list);
    Student alex{"alex", 1};
    list.push_back(std::move(alex));
    list.push_back(std::move(Student{"bill", 2}));
    list.push_back(Student{"bill2", 3});

    Student chille{"chille", 3};
    printf("chille %x\n", (int *)&chille);
    list.emplace_back(std::move(chille));
    list.emplace_back(Student{"chille2", 3});
    list.emplace_back(std::move(Student{"dived", 4}));
    list.emplace_back("element", 5);

    return list;
}
void test03()
{
    vector<Student> list = create_list_student();
    printf("%x \n", (int *)&list);
    // for (auto s : list) // 拷贝构造，深拷贝
    // {
    //     printf("name=%s, age=%d\n", s.name.c_str(), s.age);
    // }

    for (int i = 0; i < list.size(); ++i)
    {
        printf("name=%s, age=%d\n", list[i].name.c_str(), list[i].age);
    }
    printf("chille %x\n", (int *)&list[4]);
}

int main(int argc, char *argv[])
{
    // test01();
    test03();

    return 0;
}